var pageParams;

function showSubList(id, Code) {
    $$.goUrl("/base/timer_data/timer_data_list.html?pId=" + id + "&pCode=" + Code)
}

function refresh() {
    $("#dataTable").bootstrapTable("removeAll");
    $("#dataTable").bootstrapTable('refresh');
}

//搜索以前将页码换成第一页
function searchBeforeInit() {
    var options = $("#dataTable").bootstrapTable("getOptions");
    options.pageNumber = 1;
    $("#dataTable").bootstrapTable('refreshOptions', options);
}

function initSearchEvent() {
    $("#serachBtn").click(function () {
        searchBeforeInit();
        refresh();
    });
    $('#serachBtn').bind('keypress', function (e) {
        if (e.keyCode == 13) {
            searchBeforeInit();
            refresh();
        }
    });
    $('#seachValue1').bind('keypress', function (e) {
        if (e.keyCode == 13) {
            searchBeforeInit();
            refresh();
        }
    });
    $('#seachValue2').bind('keypress', function (e) {
        if (e.keyCode == 13) {
            searchBeforeInit();
            refresh();
        }
    });
    $("#searchReset").on("click", function () {
        //$("#searchForm")[0].reset();
        $("#searchForm input[type='text']").val("");
    });
    $("#del-btn").click(function () {
        var len = $("#dataTable input:checked").length;
        if (len == 0) {
            //未选中数据提示框
            layer.msg('请选择数据', {time: 1000});
        } else {
            deleteData();
        }
    });
    //新建弹框
    $("#add-btn").on('click', function () {
        addData();
    });

}

function editData(rowId) {
    layer.open({
        move: false,
        type: 2,
        title: '新建',
        area: ['800px', '610px'],
        content: 'timer_update.html?rowId=' + rowId,
        btn: ['保存', '取消'],
        btnAlign: 'c',
        yes: function (index, layero) {
            var openWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
            var ret = openWin.yesOperator();
            if (ret) {
                layer.close(index);
                refresh();
            }
        }, cancel: function (index) {
            layer.close(index);
        }
    });
}

function deleteData() {
    layer.confirm('确定要删除这条数据吗？',
        {icon: 3, title: '提示', area: '300px', btnAlign: 'c'}, function (index) {
            var rows = $("#dataTable").bootstrapTable('getSelections');
            var rowIds = [];
            for (var i = 0; i < rows.length; i++) {
                rowIds.push(rows[i].id);
            }
            $$.ajax({
                url: "/rest/BaseTimerBusiness/remove",
                data: {
                    id: rowIds.join(',')
                },
                success: function () {
                    refresh();
                }
            });
            layer.close(index);
        });
}

function addData() {
    layer.open({
        move: false,
        type: 2,
        title: '新建',
        area: ['800px', '610px'],
        content: 'timer_add.html',
        btn: ['保存', '取消'],
        btnAlign: 'c',
        yes: function (index, layero) {
            var openWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
            var ret = openWin.yesOperator();
            if (ret) {
                layer.close(index);
                refresh();
            }
        }, cancel: function (index) {
            layer.close(index);
        }
    });
}

$(function () {
    pageParams = $$.getQueryString();
    //$('#deviceCode').focus();
    initSearchEvent();
    //渲染表格
    var columns = deviceList.columnsInit();
    var table = deviceList.gridInit(columns);
})
;(function (undefined) {
    "use strict"
    var _global;

    function result(args, fn) {
        var argsArr = Array.prototype.slice.call(args);
        if (argsArr.length > 0) {
            return argsArr.reduce(fn);
        } else {
            return 0;
        }
    }

    var deviceList = {
        columnsInit: function () {
            return [
                {
                    field: 'status',
                    title: '序号',
                    formatter: function (value, row, index) {
                        return index + 1;
                    }
                },
                {field: "timerName", title: "定时器名称", sortable: true},
                {field: "timerCycle", title: "时间周期"},
                {field: "timerUnit", title: "单位" ,
                    formatter: function (value, row, index) {
                    var ret;

                        if(value==1){
                            ret="分";
                        }else if(value==2){
                            ret="时";
                        }else if(value==3){
                            ret="天";
                        }else if(value==4){
                            ret="周";
                        }else if(value==5){
                            ret="月";
                        }
                        return ret;
                    }
                },
                {field: "timerStart", title: "定时开始时间"},
                {field: "timerOrigin", title: "定时参照时间"},
                {field: "timerGroup", title: "分组"},
                {field: "timerBusi", title: "执行业务服务"},
                {field: "rowSort", title: "排序号"},
                {field: "remarks", title: "备注"},

                {
                    field: 'status',
                    title: '状态',
                    formatter: function (value, row, index) {
                        if (value == '1') {
                            return [
                                '<span class="sys-type-bg pro-status-green">正常</span>'
                            ];
                        }
                        return [
                            '<span class="sys-type-bg pro-status-red">禁用</span>'
                        ];
                    }
                },
                {
                    field: 'optionDel',
                    checkbox: true
                }, {
                    field: 'option',
                    title: '操作',
                    width: 80,
                    align: 'center',
                    formatter: function (value, row, index) {
                        return [
                            '<div class="opt-dropdown tb-opt-div"><a class="tb-opt-icon" href="javascript:void(0)">操作 <i class="glyphicon glyphicon-triangle-bottom"></i></a>',
                            '<ul class="opt-dropdown-menu opt-dropdown-menu-bottom">',
                            '<li><a href="javascript:editData(\'' + row.id + '\')" title="编辑">',
                            '<i class="glyphicon glyphicon-pencil"></i><em> 编辑</em></a></li>',
                            '<li><a href="javascript:showSubList(\'' + row.id + '\',\'' + row.metaCode + '\')"><i class="glyphicon glyphicon-th-list"></i><em> 明细</em></a></li></ul></div>'

                        ].join('');
                    }
                }]
        },
        gridInit: function (columns) {
            return $("#dataTable").bootstrapTable({
                method: "post",
                contentType: "application/x-www-form-urlencoded",
                url: $$.restServerUrl + '/bootstrap/basegrid/BaseTimerBusiness/list',
                columns: columns,
                undefinedText: '-',
                striped: 'true',
                classes: 'table table-hover qxttable',
                pagination: 'true',
                clickToSelect: true,
                singleSelect: false,
                sidePagination: "server",
                queryParamsType: "page",
                sortSide: 'client',
                sortable: true,
                pageNumber: 1,
                pageSize: 10,
                idField: 'id',
                silentSort: true,
                pageList: [
                    10, 20, 50, 100
                ],
                onClickRow: function (row, $element, field) {
                    $("#dataTable").bootstrapTable("uncheckAll");
                },
                queryParams: function queryParams(params) {   //设置查询参数
                    var args = {};
                    args.c_timerGroup_7=$("#seachValue1").val();
                    args.c_timerName_7=$("#seachValue2").val();
                    args.page = params.pageNumber - 1; //    params.pageNumber
                    args.pageSize = params.pageSize;     //params.pageSize
                    args.isPage = 1;
                    //args.order=orderStr;
                    var paramStr = JSON.stringify(args);
                    //alert(params.pageNumber+":"+params.pageSize);
                    //alert(orderStr);
                    var param = {
                        params: paramStr
                    };
                    return param;
                },
                onPostBody: function () {
                    $(".opt-dropdown").each(function (index, data) {
                        var _this = this;
                        $(this).hover(function () {
                            $(_this).find(".opt-dropdown-menu").show();
                        }, function () {
                            $(_this).find(".opt-dropdown-menu").hide();
                        })
                    });
                }
            })
        },
        dataInit: function () {
            return []
        }
    }
    // 将插件对象暴露给全局对象
    _global = (function () {
        return this || (0, eval)('this');
    }());
    if (typeof module !== "undefined" && module.exports) {
        module.exports = deviceList;
    } else if (typeof define === "function" && define.amd) {
        define(function () {
            return deviceList;
        });
    } else {
        !('plugin' in _global) && (_global.deviceList = deviceList);
    }
}());
